import React from 'react'
import { Switch, Route } from 'react-router-dom'

/**
 * 渲染路由
 * @param {*} routes 路由，App.jsx 中所接收到的路由封装
 * @param {*} extraProps  可选参数
 * @param {*} switchProps 可选参数
 * @returns 
 */
function renderRoutes(routes, extraProps = {}, switchProps = {}) {
    return routes ? (
        <Switch {...switchProps}>
            {routes.map((route, i) => (
                <Route
                    key={route.key || i}
                    path={route.path}
                    exact={route.exact}
                    strict={route.strict}
                    render={(props) =>
                        route.render ? (
                            route.render({ ...props, ...extraProps, route: route })
                        ) : (
                            <route.component {...props} {...extraProps} route={route} />
                        )
                    }
                />
            ))}
        </Switch>
    ) : null
}

export default renderRoutes
